package st.baseline;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Baseline {

	
	private static ArrayList<String> wordList = new ArrayList<String>();
	private static Map<String, String> wordTag = new HashMap<String, String>();
	private static Map<String, Integer> wordTagCount = new HashMap<String, Integer>();
	private static String unKnownWord = "<UNK>";
	private static StringBuffer trainset = new StringBuffer();
	
	public static void main(String[] args) {
		// Train the model
		readFile("train.pos");
	    countTags();
        createModel();
        
        //Test the model
        test("test-obs.pos");
	}
	
	public static void createModel(){
		
		Map<String, Integer> temp = new HashMap<String, Integer>();
		Set<String> wordSet = wordTagCount.keySet();
		Iterator<String> iter = wordSet.iterator();
		
		while(iter.hasNext()){
			
			String newword = iter.next();
			String word = newword.split(" ")[0];
			String tag = newword.split(" ")[1];
			Integer count = wordTagCount.get(newword);
			
			if(!wordTag.containsKey(word)){
			   wordTag.put(word, tag);
			   temp.put(word, count);
			}else{
				
			   Integer oldCount = temp.get(word);
			   if(count.intValue() > oldCount.intValue()){
				   wordTag.put(word, tag);
				   temp.put(word, count);
			   }
			}
		}
	}
	
	public static void countTags(){
		
		
		int size = wordList.size();
		int trainingSize = 4 * size / 5;
		
		for (int count = 0; count < trainingSize; count++) {
			String newWord = wordList.get(count);
			trainset.append(newWord.split(" ")[0]+" ");
			if (wordTagCount.containsKey(newWord)) {
				
				int tagCount = wordTagCount.get(newWord).intValue()+1;
				wordTagCount.put(newWord,new Integer(tagCount));
			}else{
				wordTagCount.put(newWord,new Integer(1));
			}
		}
		
		for (int count = trainingSize; count < size; count++) {
            
			String newWord = wordList.get(count);
			
			if (wordTagCount.containsKey(newWord)) {
				
				int tagCount = wordTagCount.get(newWord).intValue()+1;
				wordTagCount.put(newWord,new Integer(tagCount));
				
			}else{
				
				String word = newWord.split(" ")[0];
				String tag = newWord.split(" ")[1];
			
				if(trainset.toString().contains(word)){
					
					wordTagCount.put(newWord,new Integer(1));
					
				}else{
				
				String unkTag = unKnownWord+" "+tag;
				if (wordTagCount.containsKey(unkTag)) {
					
					int tagCount = wordTagCount.get(unkTag).intValue()+1;
					wordTagCount.put(unkTag,new Integer(tagCount));
				}else{
				wordTagCount.put(unkTag,new Integer(1));
				}
				}
			}
		}
		
	}
	
	public static void test(String filename){
		BufferedWriter bw = null;
		BufferedReader br = null;
		try {
			bw = new BufferedWriter(new FileWriter("baseline.txt"));
			br = new BufferedReader(new FileReader(filename));
			String word = null;
			while ((word = br.readLine()) != null) {
				
				if(wordTag.containsKey(word.toLowerCase())){
					bw.write(wordTag.get(word.toLowerCase())+" "+word);
				}else{
					bw.write(wordTag.get(unKnownWord)+" "+word);
				}
				
				
				bw.newLine();
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				br.close();
				bw.flush();
				bw.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	
	public static void readFile(String filename){
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(filename));
			String word = null;
			while ((word = br.readLine()) != null) {
				String[] split = word.split(" ");
				String newWord = split[1].toLowerCase()+" "+split[0];
				
				wordList.add(newWord);
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
			br.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
			
	}

}
